package com.azkj.album.view.activity;


import android.content.Intent;
import android.os.Bundle;

import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;
import android.widget.ImageView;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import androidx.viewpager.widget.PagerAdapter;
import androidx.viewpager.widget.ViewPager;


import com.alibaba.fastjson.JSON;
import com.azkj.album.R;
import com.bumptech.glide.Glide;

import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.List;


public class GuideActivity extends AppCompatActivity {

    private MyPagerAdapter adapter;
    private List<ImageView> mDatas;
    private ViewPager vp_welcome;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        requestWindowFeature(Window.FEATURE_NO_TITLE);
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);

        setContentView(R.layout.activity_guide);


        List<String> data = JSON.parseArray(getIntent().getStringExtra("imgs"), String.class);
        mDatas = new ArrayList<>();

        for (int i = 0; i < data.size(); i++) {

            ImageView iv = getImageView();
            if (i == data.size() - 1) {
                iv.setOnClickListener(v -> {
                    Intent intent = new Intent(GuideActivity.this, LoginActivity.class);
                    GuideActivity.this.startActivity(intent);
                    GuideActivity.this.finish();
                });
            }

            Glide.with(this).load(data.get(i)).into(iv);
            mDatas.add(iv);

        }

        initAdapter();
    }

    private ImageView getImageView() {
        ImageView imageView = new ImageView(this, null);
        imageView.setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,
                ViewGroup.LayoutParams.MATCH_PARENT));
        imageView.setScaleType(ImageView.ScaleType.FIT_XY);
        return imageView;
    }

    private void initAdapter() {
        vp_welcome = findViewById(R.id.vp_welcome);
        adapter = new MyPagerAdapter(mDatas);
        vp_welcome.setAdapter(adapter);
        vp_welcome.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {

            private boolean in = false;

            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
                if (position == 3 && positionOffsetPixels > 100 && !in) {
                    in = true;
                    Intent intent = new Intent(GuideActivity.this, LoginActivity.class);
                    GuideActivity.this.startActivity(intent);
                    GuideActivity.this.finish();
                }
            }

            @Override
            public void onPageSelected(int position) {
                if (position == 3) {
                    try {
                        Field field = ViewPager.class.getDeclaredField("mTouchSlop"); // 通过ViewPager类得到字段，不能通过实例得到字段。
                        field.setAccessible(true); // 设置Java不检查权限。
                        field.setInt(vp_welcome, 5); // 设置字段的值，此处应该使用ViewPager实例。设置只有滑动长度大于150px的时候，ViewPager才进行滑动
                    } catch (Exception e2) {
                        e2.printStackTrace();
                    }
                }

            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });
    }

    class MyPagerAdapter extends PagerAdapter {


        private List<ImageView> mDatas;

        public MyPagerAdapter(List<ImageView> mDatas) {

            this.mDatas = mDatas;
        }

        @Override
        public int getCount() {
            return mDatas.size();
        }

        @Override
        public boolean isViewFromObject(View view, Object object) {
            return view.equals(object);
        }


        @Override
        public void destroyItem(ViewGroup container, int position, Object object) {
            ((ViewPager) container).removeView((ImageView) object);
        }


        @Override
        public ImageView instantiateItem(ViewGroup container, int position) {
            container.addView(mDatas.get(position));
            return mDatas.get(position);
        }

    }
}

